package com.csit.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="T_Buy")
public class Buy implements Serializable {

	private static final long serialVersionUID = 5729975451633548125L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer buyID;
	
	@Column(length=50)
	private String buyCode;
	
	@Temporal(TemporalType.DATE)
	private Date buyDate;
	
	@Column(length=100)
	private String buyer;
	
	@Column(length=1000)
	private String note;
	
	@Column(updatable=false,nullable=false)
	@Temporal(TemporalType.DATE)
	private Date createDate;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="supplierID")
	private Supplier supplier;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="storeHouseID")
	private DataDictionary storeHouse;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="shopID",updatable=false)
	private DataDictionary shop;

	@OneToMany(fetch=FetchType.LAZY,mappedBy="buy")
	@OrderBy("position")
	private List<BuyDetail> buyDetailList = new ArrayList<BuyDetail>();
	
	public Integer getBuyID() {
		return buyID;
	}

	public void setBuyID(Integer buyID) {
		this.buyID = buyID;
	}

	public String getBuyCode() {
		return buyCode;
	}

	public void setBuyCode(String buyCode) {
		this.buyCode = buyCode;
	}

	public Date getBuyDate() {
		return buyDate;
	}

	public void setBuyDate(Date buyDate) {
		this.buyDate = buyDate;
	}

	public String getBuyer() {
		return buyer;
	}

	public void setBuyer(String buyer) {
		this.buyer = buyer;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public DataDictionary getStoreHouse() {
		return storeHouse;
	}

	public void setStoreHouse(DataDictionary storeHouse) {
		this.storeHouse = storeHouse;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public DataDictionary getShop() {
		return shop;
	}

	public void setShop(DataDictionary shop) {
		this.shop = shop;
	}

	public List<BuyDetail> getBuyDetailList() {
		return buyDetailList;
	}

	public void setBuyDetailList(List<BuyDetail> buyDetailList) {
		this.buyDetailList = buyDetailList;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	@Transient
	public void setSupplierID(Integer supplierID){
		if(supplier==null){
			supplier = new Supplier();
		}
		supplier.setSupplierID(supplierID);
	}
	@Transient
	public void setStoreHouseID(Integer storeHouseID){
		if(storeHouse==null){
			storeHouse = new DataDictionary();
		}
		storeHouse.setDataDictionaryID(storeHouseID);
	}
	@Transient
	public void setShopID(Integer shopID){
		if(shop==null){
			shop = new DataDictionary();
		}
		shop.setDataDictionaryID(shopID);
	}
	
	@Transient
	public String getSupplierName(){
		if(supplier==null){
			return null;
		}
		return supplier.getSupplierName();
	}
	@Transient
	public String getStoreHouseName(){
		if(storeHouse==null){
			return null;
		}
		return storeHouse.getDataDictionaryName();
	}

}
